package com.yc.springmvc.web;

import com.yc.springmvc.bean.Result;
import com.yc.springmvc.biz.BizException;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MaxUploadSizeExceededException;

// 实现全局异常拦截
@Component
@ControllerAdvice
public class MyExceptionHandler {

    // 异常拦截方法
    @ExceptionHandler(MaxUploadSizeExceededException.class)
    @ResponseBody
    public Result handleMaxUploadSizeExceededException(MaxUploadSizeExceededException e){
        System.out.println(e.getClass().getSimpleName());
        return new Result(0, "上传文件大小超过设定值( 1MB )!", null);
    }

    // 业务异常
    @ExceptionHandler(BizException.class)
    @ResponseBody
    public Result handleBizException(BizException e){
        e.printStackTrace();
        return new Result(0, e.getMessage(), null);
    }

    // 系统基础异常
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result handleException(Exception e){
        e.printStackTrace();
        return new Result(0, "系统繁忙, 请稍后再试!", null);
    }
}
